package com.exchangeek.server.adapter;

/**
 * Created with IntelliJ IDEA.
 * Project Name: Exchangeek_Server
 * Date Created: 2013/09/14 下午3:51
 *
 * @author Wings
 */

import com.exchangeek.server.entity.config.SqlConnectionConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MysqlConnector extends SqlConnector {

    public MysqlConnector(SqlConnectionConfig config) {
        super(config);
    }

    @Override
    public Statement connect() {
        String driver = this.config.getDriver();// 驱动程序名
        String databaseUrl = this.config.getDatabaseUrl();
        String user = this.config.getUserName();
        String password = this.config.getPassword();
        try {
            // 加载驱动程序
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(databaseUrl, user, password);
            if (!conn.isClosed())
                System.out.println("Successfully connected to the database:" + this.config.getDatabaseUrl());
            return conn.createStatement();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}